<?php 
/**
* @orm tableName forum_topics
* @orm listener afterInsert afterInsert
* @orm listener beforeDelete beforeDelete
*/
class ForumTopic extends ForumPostBase {
	
	/**
	* @orm has parent ForumThread inverse topics
	*/
	public $thread;
	
	
	
	/**
	* @orm owns many ForumPost inverse topic
	* @orm listener afterAdd afterAddPost
	*/
	public $posts;
	/**
	* @orm int(11)
	*/
	public $nrOfPosts = 0;
	/**
	* @orm has child ForumPost
	*/
	public $lastPost;
	/**
	* @orm plugin DomArDateTime(now)
	*/
	public $lastPostDatetime;
	

	
	
	/**
	* @orm int(11) index
	*/
	public $views = 0;
	
	
	
	public function afterAddPost( ForumPost $post ) {
		$this->lastPost = $post;	
		$this->lastPostTime = $post->datetime->datetime();
		$this->nrOfPosts++;
		
		$this->thread->afterAddPost( $post );
	}
	
	public function beforeDeletePost( ForumPost $post ) {
		$this->nrOfPosts--;
		$this->thread->beforeDeletePost( $post );
		$this->save();
	}
	
	public function beforeDelete() {
		$this->thread->beforeDeletePost( $this );
		
		if( $this->user ) {
			$this->user->posts--;
			$this->user->save();
		}
		
	}

	public function afterInsert() {
		if( $this->user ) {
			$this->user->posts++;
			$this->user->save();
		}
	}
	
	public function canAddImage( $image ) {
		return parent::canAddImage($image);
	}
	
	public function afterAddImage() {
		parent::afterAddImage();
	}
	
	public function afterRemoveImage() {
		parent::afterRemoveImage();
	}
	

}
?>